<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\qrcode\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use app\admin\model\File;
use app\admin\model\Config as ConfigModel;
/**
 * 配置
 */
class Config extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $name = 'app_qrcode';
        if ($this->request->isPost()) {
            $config = input('post.value');
            ConfigModel::setVal($name, '生成带logo二维码', $config);
	    	$url = $config['url']; //填写你的链接
	    	$width = $config['size']; // 请填写生成二维码大小
	    	$path = plugin_path() . 'qrcode/';
	    	require_once $path.'/addons/qrcode.php';
			$logoFile =	strstr($config['logo'], 'http') ? $config['logo'] : public_path().$config['logo'];
			$qrFile = $path . "qrcode.png";//已经生成的原始二维码图
			$size = floor($width / 25 * 100) / 100 + 0.01;// 宽度计算方法
	    	$margin = 0; // 请填写二维码的间距 
			\QRcode::png($url, $qrFile, QR_ECLEVEL_L, $size, $margin);
			$qrFile = imagecreatefromstring(file_get_contents($qrFile));
			$logoFile = imagecreatefromstring(file_get_contents($logoFile)); 
			$qrWidth = imagesx($qrFile);//二维码图片宽度 
			$qrHeight = imagesy($qrFile);//二维码图片高度 
			$logoWidth = imagesx($logoFile);//logo图片宽度 
			$logoHeight = imagesy($logoFile);//logo图片高度 
			$logoQrWidth = $qrWidth / 5; 
			$scale = $logoWidth/$logoQrWidth; 
			$logoQrheight = $logoHeight/$scale; 
			$fromwidth = ($qrWidth - $logoQrWidth) / 2; 
			//重新组合图片并调整大小 
			imagecopyresampled($qrFile, $logoFile, $fromwidth, $fromwidth, 0, 0, $logoQrWidth, $logoQrheight, $logoWidth, $logoHeight);
            // 保存图片
			$file = public_path() . 'upload/image/' . date('Ymd') . md5(microtime(true).'qrcode') . '.png';
			imagepng($qrFile, $file);
			// 存入数据库
            $save = File::create([
                'title'       => '二维码.png',
                'type'        => 'image',
                'size'        => $config['size'],
                'url'         => str_replace(public_path(), '/', $file),
                'status'      => 1,
                'theme'       => theme(),
                'create_time' => date('Y-m-d H:i:s'),
            ]);
            // 封面图片
            thumbnail($save['url'],100,100);
			// 上传结束钩子
            event('UploadEnd', $save);
            return json(['status' => 'success', 'message' => '生成成功', 'data' => str_replace(public_path(), $this->request->domain() . '/', $file)]);
        } else {
            $config = ConfigModel::getVal($name);
            if (! $config) {
                $config = [
                    'logo'  => '',
                    'url'   => '',
                    'size'  => 200,
                ];
            }
            View::assign('config', $config);
            return View::fetch();
        }
    }
}